package com.pengcheng.taskapplication.mvp.view.fragment;

import android.content.Intent;
import android.media.Image;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.NoticeAdapter;
import com.pengcheng.taskapplication.adapter.ParkAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.NoticeModel;
import com.pengcheng.taskapplication.model.ParkOrderModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.activity.ConfrimOrderActivity;
import com.pengcheng.taskapplication.mvp.view.activity.NoticeDetailActivity;
import com.pengcheng.taskapplication.mvp.view.activity.NoticeHtml5Activity;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;

public class NoticeListFragment extends BaseFragment implements IView{
    private UserPresenterV2 userPresenter;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView pullToRefreshListView;
    ArrayList<NoticeModel> list =new ArrayList<>();
    private NoticeAdapter adapter;

    @BindView(R.id.tv_center_title)
    TextView tv_center_title;
    @BindView(R.id.img_back)
    ImageView imgBack;

    private int page=0;
    private int pagesize=10;
    private String uid;
    private String newtoken;

    @Override
    protected int layoutResID() {
        return R.layout.fragment_notice_list_;
    }

    @Override
    protected void initData() {
        tv_center_title.setText("消息公告");
        imgBack.setVisibility(View.INVISIBLE);
        uid= SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken= SPUtils.getString(context,ConstantValue.SpType.newtoken);
        userPresenter = new UserPresenterV2(this);
        //adapter
        adapter = new NoticeAdapter(context,list);
        //manager
        pullToRefreshListView.setAdapter(adapter);

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                NoticeModel model=(NoticeModel) adapterView.getAdapter().getItem(i);
                Intent intent =new Intent(context, NoticeDetailActivity.class);
                intent.putExtra("model",model);
                startActivity(intent);
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                list.clear();
                queryNotice(newtoken,uid,page,pagesize);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                queryNotice(newtoken,uid,page,pagesize);
            }
        });

        queryNotice(newtoken,uid,page,pagesize);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.queryNotice)){
            Gson gson= new Gson();
            Type type =new TypeToken<ArrayList<NoticeModel>>(){}.getType();
            ArrayList<NoticeModel> orderModels= gson.fromJson(gson.toJson(data),type);
            list.addAll(orderModels);
            adapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        pullToRefreshListView.onRefreshComplete();
    }
    public void queryNotice(String token,String uid,int page,int pagesize){
        userPresenter.getNotcieList(token,uid,page,pagesize,ConstantValue.RequestKey.queryNotice);
    }


    @Override
    public void onResume() {
        super.onResume();
        uid = SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken = SPUtils.getString(context, ConstantValue.SpType.newtoken);
    }
}
